package com.bawei.module_mine.http.viewModel

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.bawei.lib_mvi.base.BaseViewModel
import com.bawei.lib_storage.mmkv.AuthMMKV
import com.bawei.module_mine.http.intent.LoginIntent
import com.bawei.module_mine.http.repository.LoginRepository
import com.bawei.module_mine.http.state.LoginState
import com.bawei.module_mine.model.AddAccountModel
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

class LoginViewModel : BaseViewModel<LoginIntent>() {
    private val repository: LoginRepository by lazy { LoginRepository() }
    private val _state = MutableStateFlow<LoginState>(LoginState.Loading)
    val state: StateFlow<LoginState> = _state.asStateFlow()
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect {
                when (it) {
                    is LoginIntent.Login -> getLogin(it.username, it.password)
                    is LoginIntent.AddAccount -> getAddAccount(it.body)
                    is LoginIntent.FindAccountById -> getFindAccountById(it.id)
                    is LoginIntent.Like -> getLike(it.sourceflag, it.userId)
                    is LoginIntent.SetImg -> setImg(it.id, it.img)
                    is LoginIntent.QuickLogin -> quickLogin(it.exId, it.loginToken)
                    is LoginIntent.Like -> getLike(it.sourceflag,it.userId)
                    is LoginIntent.SetImg -> setImg(it.id,it.img)
                    LoginIntent.Mine -> getMine()
                }
            }
        }
    }

    private fun quickLogin(exId: String, loginToken: String) {
        viewModelScope.launch {
            repository.quickLogin(exId, loginToken)
                .catch {
                    _state.value = LoginState.Error(it.message.toString())
                }
                .filter {
                    it != null
                }
                .collect {
                    if (it.code == 200){
                        _state.value = LoginState.QuickLoginSuccess(it.data)
                        AuthMMKV.token = it.data.token
                        AuthMMKV.uId = it.data.id
                        AuthMMKV.username = it.data.username
                    }else{
                        _state.value = LoginState.Error(it.msg)
                    }
                }
        }
    }

    private fun getLogin(username: String, password: String) {
        viewModelScope.launch {
            repository.getLogin(username, password)
                .catch { e ->
                    _state.value = LoginState.Error(e.message.toString())
                }
                .filter { it != null }
                .collect {
                    if (it.code == 200) {
                        if (it.data == null) {
                            _state.value = LoginState.Error("登录失败")
                        } else {
                            _state.value = LoginState.Success(it.data)
                            AuthMMKV.token = it.data.token
                            AuthMMKV.uId = it.data.id
                            AuthMMKV.username = it.data.username
                            AuthMMKV.password = it.data.password
                        }
                    } else {
                        _state.value = LoginState.Error(it.msg)
                    }
                }
        }
    }

    private fun getAddAccount(body: AddAccountModel) {
        viewModelScope.launch {
            repository.getAddAccount(body)
                .catch { e ->
                    _state.value = LoginState.Error(e.message.toString())
                }
                .filter { it != null }
                .collect {
                    if (it.code == 200) {
                        if (it.data == null) {
                            _state.value = LoginState.Error("添加账户失败")
                        } else {
                            _state.value = LoginState.AddAccountSuccess(it.data)
                            AuthMMKV.accountId = it.data.id
                        }
                    } else {
                        _state.value = LoginState.Error(it.msg)
                    }
                }
        }
    }

    private fun getFindAccountById(id: Int) {
        viewModelScope.launch {
            repository.getFindAccountById(id)
                .catch { e ->
                    _state.value = LoginState.Error(e.message.toString())
                }
                .filter { it != null }
                .collect {
                    if (it.code == 200) {
                        if (it.data == null) {
                            _state.value = LoginState.Error("获取账户数据失败")
                        } else {
                            _state.value = LoginState.AddAccountSuccess(it.data)
                        }
                    } else {
                        _state.value = LoginState.Error(it.msg)
                    }
                }
        }
    }

    private fun getLike(sourceflag: String, userId: Int) {
        viewModelScope.launch {
            repository.getLike(sourceflag, userId)
                .catch { e ->
                    _state.value = LoginState.Error(e.message.toString())
                }
                .filter { it != null }
                .collect {
                    if (it.code == 200) {
                        if (it.data == null) {
                            _state.value = LoginState.Error("获取点赞数据失败")
                        } else {
                            _state.value = LoginState.LikeSuccess(it.data)
                        }
                    } else {
                        _state.value = LoginState.Error(it.msg)
                    }
                }
        }
    }

    private fun setImg(id: Int, img: String) {
        viewModelScope.launch {
            repository.setImg(id, img)
                .catch { e ->
                    _state.value = LoginState.Error(e.message.toString())
                }
                .filter { it != null }
                .collect {
                    if (it.code == 200) {
                        if (it.data == null) {
                            _state.value = LoginState.Error("修改头像失败")
                        } else {
                            ToastUtils.showLong("修改成功")
                        }
                    } else {
                        _state.value = LoginState.Error(it.msg)
                    }
                }
        }
    }

    private fun getMine() {
        viewModelScope.launch {
            repository.getMine()
                .catch { e ->
                    _state.value = LoginState.Error(e.message.toString())
                }
                .filter { it != null }
                .collect {
                    if (it.code == 200) {
                        if (it.data == null) {
                            _state.value = LoginState.Error("获取数据失败")
                        }else{
                            _state.value = LoginState.MineSuccess(it.data)
                        }
                    }
                }
        }
    }
}










































